//
//  main.m
//  Demo0023
//
//  Created by zTercel on 13-3-5.
//  Copyright (c) 2013年 zTercel. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSFileManager* fm = [NSFileManager defaultManager];
        
      // [fm removeItemAtPath:[fm currentDirectoryPath] error:nil];
        [fm createDirectoryAtPath:@"test" withIntermediateDirectories:YES attributes:nil error:nil];
         NSString* currentDirectory;
        if ([fm changeCurrentDirectoryPath:@"test"])
        {
            currentDirectory = [fm currentDirectoryPath];
            NSLog(@"new current Directory: %@", currentDirectory);
        }
        
        // create directorys & files
        NSArray* dirNames   = [NSArray arrayWithObjects:@"test1", @"tests2", @"test3", nil];
        NSArray* fileNames  = [NSArray arrayWithObjects:@"test1.txt", @"tests2.doc", @"test3.cpp", nil];
        if ([dirNames count] == [fileNames count])
        {
            for (int ii = 0; ii < [dirNames count]; ii++)
            {
                [fm createDirectoryAtPath:[dirNames objectAtIndex:ii] withIntermediateDirectories:YES attributes:nil error:nil];
                [fm changeCurrentDirectoryPath:[dirNames objectAtIndex:ii]];
                [fm createFileAtPath:[fileNames objectAtIndex:ii] contents:nil attributes:nil];
                [fm changeCurrentDirectoryPath:currentDirectory];
            }
        }
        
        // enumerator directory
        NSString* path      = NULL;
        BOOL isDirectory    = NO;
        NSDirectoryEnumerator* dirEnum = [fm enumeratorAtPath:currentDirectory];
        NSLog(@"enumeratorAtPath enum directory:");
        while (path = [dirEnum nextObject])
        {
            [fm fileExistsAtPath:path isDirectory:&isDirectory];
            if (isDirectory)
            {
                NSLog(@"%@ dir", path);
            } else {
                NSLog(@"%@", path);
            }
        }
        
        NSLog(@"contentsOfDirectoryAtPath enum directory:");
        NSArray* content = [fm contentsOfDirectoryAtPath:currentDirectory error:nil];
        for (NSString* string in content) {
            [fm fileExistsAtPath:string isDirectory:&isDirectory];
            NSLog(@"%@ %@", string, isDirectory ? @"dir" : @"");
        }
        
        // rename directory
        [fm moveItemAtPath:@"test1" toPath:@"ztercel" error:nil];
        
        [fm copyItemAtPath:@"test2" toPath:@"ztercel/" error:nil];
        
        // remove directory
        if ([fm removeItemAtPath:currentDirectory error:nil])
        {
            NSLog(@"remove successfully");
        }
        
        NSLog(@"full User Name: %@", NSFullUserName());
        NSLog(@"user Name: %@", NSUserName());
        NSLog(@"Home directory: %@", NSHomeDirectory());
        NSLog(@"Home directory for user: %@", NSHomeDirectoryForUser(@"ztercel"));
        NSLog(@"temp directory: %@", NSTemporaryDirectory());
        

    }
    return 0;
}

